package com.c2b.algorithm.leetcode.base;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <a href='https://leetcode.cn/problems/binary-tree-paths/'>二叉树的所有路径(Binary Tree Paths)</a>
 * <p>给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。</p>
 * <p>叶子节点 是指没有子节点的节点。</p>
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *              1
 *             / \
 *            2   3
 *             \
 *              5
 *      输入：root = [1,2,3,null,5]
 *      输出：["1->2->5","1->3"]
 *
 * 示例 2：
 *      输入：root = [1]
 *      输出：["1"]
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 * <ul>
 *     <li>树中节点的数目在范围 [1, 100] 内</li>
 *     <li>-100 <= Node.val <= 100</li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/5/19 16:43
 */
public class LC0257BinaryTreePaths_S {

    static class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> allPathList = new ArrayList<>();
            if (root == null) {
                return allPathList;
            }
            //binaryTreePathsByRecursion(root, "", allPathList);
            binaryTreePathsByIteration(root, allPathList);
            return allPathList;
        }

        private void binaryTreePathsByRecursion(TreeNode currNode, String currPath, List<String> allPathList) {
            // 到达叶子节点，找到一条路径
            if (currNode.left == null && currNode.right == null) {
                allPathList.add(currPath + currNode.val);
            }
            if (currNode.left != null) {
                binaryTreePathsByRecursion(currNode.left, currPath + currNode.val + "->", allPathList);
            }
            if (currNode.right != null) {
                binaryTreePathsByRecursion(currNode.right, currPath + currNode.val + "->", allPathList);
            }
        }

        private void binaryTreePathsByIteration(TreeNode node, List<String> allPathList) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(node);
            Queue<String> currPaths = new LinkedList<>();
            currPaths.offer("");
            while (!queue.isEmpty()) {
                TreeNode currNode = queue.poll();
                String currPath = currPaths.poll() + currNode.val;
                if (currNode.left == null && currNode.right == null) {
                    allPathList.add(currPath);
                }
                if (currNode.left != null) {
                    queue.offer(currNode.left);
                    currPaths.offer(currPath + "->");
                }
                if (currNode.right != null) {
                    queue.offer(currNode.right);
                    currPaths.offer(currPath + "->");
                }
            }
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);
        Solution solution = new Solution();
        Printer.printListString(solution.binaryTreePaths(root));
    }
}
